package com.bjpowernode.javase;

import java.util.concurrent.atomic.AtomicLong;

public class AtomicLongTest {
    public static void main(String[] args) {

        //1 创建0初始值
        AtomicLong atomicLong1 = new AtomicLong();
        System.out.println(atomicLong1.get());
        //2 创建指定初始值
        AtomicLong atomicLong2 = new AtomicLong(3);
        System.out.println(atomicLong2.get());
        //3 以原子的方式将给定值添加到当前值，先加上特定的值在获取结果
        AtomicLong atomicLong3 = new AtomicLong(3);
        atomicLong3.addAndGet(5);
        System.out.println(atomicLong3.get());
        //4 现获取当前值，在加上特定的值
        AtomicLong atomicLong4 = new AtomicLong(3);
        long ret = atomicLong4.getAndAdd(5);
        System.out.println("ret="+ret);
        System.out.println(atomicLong4.get());
        //5 如果当前值 == 预期值，则以原子的方式将该值设置为给定的新值
        AtomicLong atomicLong5 = new AtomicLong(3);
        atomicLong5.compareAndSet(3,10);
        System.out.println(atomicLong5);
        //6 先减去1在获取值
        AtomicLong atomicLong6 = new AtomicLong(3);
        atomicLong6.decrementAndGet();
        System.out.println(atomicLong6);
    }
}
